<?php
/**
 * Description: 阅读通知
 * User: devforma
 * Date: 15/4/16
 * Time: 15:12
 */

class ReadNotification implements iController {

    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {
        global $cmsdb;
        
        if (validateUser($queryArgs) !== 1)
            return genOutput(1000);

        $userId = $queryArgs['user_id'];

        if (empty($queryArgs['notification_id']) || !is_numeric($queryArgs['notification_id']))
            return genOutput(1000);

        //检验通知是否存在
        $notifyExists = $cmsdb->getVar('SELECT `id` FROM `qb_notify` WHERE `id`=:nid',
            array(':nid' => $queryArgs['notification_id']));
        if (empty($notifyExists))
            return genOutput(1000);

        //检验是否已读
        $hasRead = $cmsdb->getVar('SELECT `id` FROM `qb_relationship_user_notify` WHERE `user_id`=:uid
        AND `notify_id`=:nid', array(':uid' => $userId, ':nid' => $queryArgs['notification_id']));
        if (!empty($hasRead))
            return genOutput(1001);

        $cmsdb->insert('qb_relationship_user_notify', array(
            'notify_id' => $queryArgs['notification_id'],
            'user_id' => $userId,
            'has_read' => 1,
            'action_time' => date('Y-m-d H:i:s'),
            'extra_info' => ''
        ));

        return genOutput(1);
    }
}